home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / Ruler.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  9.2 KB  |  332 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Ruler.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Laurent Delamare
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef CONSTANT_H
  15. #include "Constant.h"
  16. #endif
  17.  
  18. #ifndef RULER_H
  19. #include "Ruler.h"
  20. #endif
  21.  
  22. #ifndef FWFRAME_H
  23. #include "FWFrame.h"
  24. #endif
  25.  
  26. #ifndef FWRECSHP_H
  27. #include "FWRecShp.h"            // FW_CRectShape
  28. #endif
  29.  
  30. #ifndef FWTXTSHP_H
  31. #include "FWTxtShp.h"            // FW_CTextShape
  32. #endif
  33.  
  34. #ifndef FWLINSHP_H
  35. #include "FWLinShp.h"            // FW_CLineShape
  36. #endif
  37.  
  38. #ifndef FWCONTXT_H
  39. #include "FWContxt.h"
  40. #endif
  41.  
  42. // ----- OpenDoc Includes -----
  43.  
  44. #ifndef SOM_ODFacet_xh
  45. #include <Facet.xh>
  46. #endif
  47.  
  48. #ifndef SOM_ODWindow_xh
  49. #include <Window.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODShape_xh
  53. #include <Shape.xh>
  54. #endif
  55.  
  56. //========================================================================================
  57. // Runtime Informations
  58. //========================================================================================
  59.  
  60. #ifdef FW_BUILD_MAC
  61. #pragma segment odfdraw
  62. #endif
  63.  
  64. FW_DEFINE_AUTO(CRuler)
  65. FW_DEFINE_CLASS_M1(CRuler, FW_CSuperView)
  66.  
  67. const FW_ClassTypeConstant FW_LRuler = FW_TYPE_CONSTANT('r','u','l','r');
  68. FW_REGISTER_ARCHIVABLE_CLASS(FW_LRuler, CRuler, CRuler::Create, FW_CView::Read, CRuler::Destroy, FW_CView::Write)
  69.  
  70. //========================================================================================
  71. // CLASS CRuler
  72. //========================================================================================
  73.  
  74. const FW_Fixed kRulerExtent = FW_IntToFixed(1000);
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // CRuler::CRuler
  78. //----------------------------------------------------------------------------------------
  79.  
  80. CRuler::CRuler(Environment* ev, 
  81.                 FW_CSuperView* container,
  82.                 const FW_CRect& bounds,
  83.                 FW_EScrollingDirection scrollDir,
  84.                 FW_Fixed zoomFactor) :
  85.     FW_CSuperView(ev, 
  86.                     container, 
  87.                     bounds, 
  88.                     0,
  89.                     (scrollDir & FW_kXScrolling) ? 
  90.                                 FW_CPoint(kRulerExtent, bounds.bottom - bounds.top)
  91.                                 : FW_CPoint(bounds.right - bounds.left, kRulerExtent),
  92.                     scrollDir),
  93.     fLineStyle(FW_kFixed0),
  94.     fFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10))
  95. {
  96.     FW_ASSERT(scrollDir != FW_kNoScrolling);
  97.     
  98.     ZoomFactorChanged(ev, zoomFactor);
  99.  
  100.     // Bind the ruler to left or top egde of its superview by default
  101.     FW_ViewBinding    rulerBindings = (IsScrollingInY(ev)) ? 
  102.                 FW_kFixedWidth + FW_kLeftBinding + FW_kTopBinding + FW_kBottomBinding :
  103.                 FW_kFixedHeight + FW_kLeftBinding + FW_kTopBinding + FW_kRightBinding;
  104.     SetBindings(ev, rulerBindings);
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // CRuler::CRuler
  109. //----------------------------------------------------------------------------------------
  110.  
  111. CRuler::CRuler(Environment* ev) :
  112.     FW_CSuperView(ev),
  113.     fLineStyle(FW_kFixed0),
  114.     fFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10))
  115. {
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // CRuler::~CRuler
  120. //----------------------------------------------------------------------------------------
  121.  
  122. CRuler::~CRuler()
  123. {
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // CRuler::ZoomFactorChanged
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void CRuler::ZoomFactorChanged(Environment* ev, FW_Fixed zoomFactor)
  131. {
  132.     if (IsScrollingInY(ev))
  133.         fFont.SetFontSize(FW_IntToFixed(10) / zoomFactor);    
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CRuler::Draw
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void CRuler::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  141. {
  142.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  143.     
  144.     FW_CRect extent(FW_kZeroPoint, GetExtent(ev));
  145.         
  146.     extent.Intersection(FW_GetShapeBoundingBox(ev, invalidShape));
  147.  
  148.     FW_CRectShape::RenderRect(vc, extent, FW_kFill, FW_kWhiteEraseInk);
  149.  
  150.     if (IsScrollingInX(ev))
  151.         RenderHorizontalRuler(vc, extent);
  152.     else
  153.         RenderVerticalRuler(vc, extent);
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. // CRuler::RenderGradation
  158. //----------------------------------------------------------------------------------------
  159.  
  160. void CRuler::RenderGradation(FW_CViewContext& vc,
  161.                                 short gradation, 
  162.                                 const FW_CPoint& pos)
  163. {
  164.     FW_CString32 string;
  165.     string.ReplaceAllAsSignedDecimalInteger(gradation);
  166.  
  167.     FW_CTextShape::RenderText(vc, string,
  168.                             FW_CPoint(pos.x - FW_kFixedPos1, pos.y - FW_kFixedPos1),
  169.                             fFont,
  170.                             FW_kTextAlignRight | FW_kTextAlignBottom);
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. // CRuler::RenderHorizontalRuler
  175. //----------------------------------------------------------------------------------------
  176.  
  177. void CRuler::RenderHorizontalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  178. {    
  179.     FW_CLineShape::RenderLine(vc, 
  180.                                 FW_CPoint(visibleRect.left, kRulerWidth - FW_kFixedPos1), 
  181.                                 FW_CPoint(visibleRect.right, kRulerWidth - FW_kFixedPos1), 
  182.                                 FW_kNormalInk, fLineStyle);
  183.  
  184.     short h = FW_FixedToInt(kRulerWidth);
  185.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  186.     FW_Fixed half = FW_IntToFixed(h / 2);
  187.     FW_Fixed third = FW_IntToFixed(h / 3);
  188.  
  189.     short short72 = 72;
  190.     
  191.     short gradation = FW_FixedToInt(visibleRect.left) / short72;
  192.     FW_Fixed minX = FW_IntToFixed(short72 * gradation);
  193.     FW_Fixed maxX = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.right + FW_IntToFixed(short72)) / short72));
  194.     
  195.     FW_Fixed fixed18 = FW_IntToFixed(18);
  196.     
  197.     short n = 0;
  198.     FW_Fixed height = full;
  199.     if (minX == FW_kFixed0)
  200.     {
  201.         minX = fixed18;
  202.         n = 1;
  203.         height = third;
  204.     }
  205.     
  206.     FW_CPoint start(minX, kRulerWidth);
  207.     FW_CPoint end(minX, kRulerWidth - height);
  208.     for (FW_Fixed x = minX; x <= maxX; x += fixed18)
  209.     {
  210.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  211.         if (n == 0)
  212.             RenderGradation(vc, gradation, start);
  213.         
  214.         n++;
  215.         if (n == 4)
  216.         {
  217.             n = 0;
  218.             gradation++;
  219.             height = full;
  220.         }
  221.         else if (n == 2)
  222.             height = half;
  223.         else if (n == 1 || n == 3)
  224.             height = third;
  225.         
  226.         start.x += fixed18;
  227.         end.x += fixed18;
  228.         end.y = kRulerWidth - height;
  229.     }
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. // CRuler::RenderVerticalRuler
  234. //----------------------------------------------------------------------------------------
  235.  
  236. void CRuler::RenderVerticalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  237. {
  238.     FW_CLineShape::RenderLine(vc, 
  239.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.top), 
  240.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.bottom), 
  241.                                 FW_kNormalInk, fLineStyle);
  242.  
  243.     short h = FW_FixedToInt(kRulerWidth);
  244.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  245.     FW_Fixed half = FW_IntToFixed(h / 2);
  246.     FW_Fixed third = FW_IntToFixed(h / 3);
  247.  
  248.     short short72 = 72;
  249.     
  250.     short gradation = FW_FixedToInt(visibleRect.top) / short72;
  251.     FW_Fixed minY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.top) / short72));
  252.     FW_Fixed maxY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.bottom + FW_IntToFixed(short72)) / short72));
  253.     
  254.     FW_Fixed fixed18 = FW_IntToFixed(18);
  255.     
  256.     short n = 0;
  257.     FW_Fixed width = full;
  258.     if (minY == FW_kFixed0)
  259.     {
  260.         minY = fixed18;
  261.         n = 1;
  262.         width = third;
  263.     }
  264.         
  265.     FW_CPoint start(kRulerWidth, minY);
  266.     FW_CPoint end(kRulerWidth - width, minY);
  267.     for (FW_Fixed y = minY; y <= maxY; y += fixed18)
  268.     {
  269.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  270.         if (n == 0)
  271.             RenderGradation(vc, gradation, start);
  272.         
  273.         n++;
  274.         if (n == 4)
  275.         {
  276.             n = 0;
  277.             gradation++;
  278.             width = full;
  279.         }
  280.         else if (n == 2)
  281.             width = half;
  282.         else if (n == 1 || n == 3)
  283.             width = third;
  284.         
  285.         start.y += fixed18;
  286.         end.y += fixed18;
  287.         end.x = kRulerWidth - width;
  288.     }
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292. //    CRuler::Create
  293. //----------------------------------------------------------------------------------------
  294.  
  295. void* CRuler::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  296. {
  297. FW_UNUSED(stream);
  298. FW_UNUSED(type);
  299.     FW_SOMEnvironment ev;
  300.     return FW_NEW(CRuler, (ev));
  301. }
  302.  
  303. //----------------------------------------------------------------------------------------
  304. //    CRuler::Destroy
  305. //----------------------------------------------------------------------------------------
  306.  
  307. void CRuler::Destroy(void* object, FW_ClassTypeConstant type)
  308. {
  309. FW_UNUSED(type);
  310.     CRuler* self = (CRuler*) object;
  311.     delete self;
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. //    CRuler::Flatten
  316. //----------------------------------------------------------------------------------------
  317.  
  318. void CRuler::Flatten(Environment* ev, FW_CWritableStream& archive) const
  319. {
  320.     FW_CSuperView::Flatten(ev, archive);
  321. }
  322.  
  323. //----------------------------------------------------------------------------------------
  324. //    CRuler::InitializeFromStream
  325. //----------------------------------------------------------------------------------------
  326.  
  327. void CRuler::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  328. {
  329.     FW_CSuperView::InitializeFromStream(ev, stream);
  330. }
  331.  
  332.